package top.vains.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import top.vains.entity.Permission;
import top.vains.entity.customer.PermissionCustomer;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author vains
 * @since 2020-02-17
 */
public interface PermissionMapper extends BaseMapper<Permission> {

    /**
     * 查询用户拥有的权限
     * @param userId 用户id
     * @return 返回权限列表
     */
    @Select("SELECT p.`name`, p.pageUrl, p.authorization FROM `user` u JOIN user_role ur ON u.user_id = ur.user_id JOIN role r ON ur.role_id = r.id JOIN role_permission rp ON r.id = rp.role_id JOIN permission p ON rp.pid = p.navId WHERE p.type = 1 AND p.deleted = 0 AND u.user_id = #{userId} GROUP BY p.pageUrl")
    List<Permission> selectPermissionsById(Integer userId);

    /**
     * 查询权限详情，包括上级节点名称
     * @return 返回详情列表
     * @param name
     */
    @Select("SELECT p1.*, IFNULL(p2.name,'根节点') AS 'parentName' FROM permission p1 LEFT JOIN permission p2 ON p1.parentId = p2.navId WHERE p1.deleted = 0 AND p1.name LIKE CONCAT('%', #{name}, '%')")
    List<PermissionCustomer> selectPermissionsDetails(String name);

}
